//
//  ExtendedTFSClient.swift
//  GitTFGUI
//
//  Created by Samuel Richard Magny on 2015-09-22.
//  Copyright © 2015 Samuel Richard Magny. All rights reserved.
//

import Cocoa
import TFSClient

public class ExtendedTFSClient: TFSClient, TFSClientDelegate {

    
    let _userSetting:UserSettings
    let _broadcastCenter:TFSClientNotificationCenter
    
    public override var workingDirectory:String {
        didSet(value)
        {
            var isValid:Bool = false
            if(isCurrentWorkingDirectoryValid())
            {
                _userSetting.workingDirectory = self.workingDirectory
                isValid = true
            }
            
            
            let notification = WorkingDirectoryChangedNotification(sender: self, directory: self.workingDirectory, isValid: isValid)
            self.sendNotification(notification)
        }
    }
    
    public override var username:String {
        didSet(value)
        {
            _userSetting.username = super.username
        }
    }
    public override var password:String {
        didSet(value)
        {
            _userSetting.password = super.password
        }
    }
    public override var email:String {
        didSet(value)
        {
            _userSetting.email = super.email
        }
    }
    
    public init(userSettings:UserSettings, broadcastCenter:TFSClientNotificationCenter) {
        _userSetting = userSettings
        _broadcastCenter = broadcastCenter
        super.init(projectDirectory: _userSetting.workingDirectory)
        
        super.username = _userSetting.username
        super.password = _userSetting.password
        super.email = _userSetting.email
        
        self.delegate = self
    }
    
    
    
    //TFSClientDelegate Implementation
    //****************************************************************
    public func commandDidStart(commandType:TFSCommandType)
    {
        let notification = CommandStartedNotification(sender: self, commandType:commandType)
        sendNotification(notification)
    }
    
    public func commandDidStop(withSuccess:Bool, command:TFSCommandType)
    {
        let notification = CommandStopedNotification(sender: self, commandType:command, isSuccessful: withSuccess)
        sendNotification(notification)
    }
    
    public func tfsClientProgressUpdate(progressInfo: String) {
        
        let notification = ProgressNotification(sender: self, progressLocalizedString: progressInfo as String)
        sendNotification(notification)
    }
    
    public func tfsClientErrorOccured(command:TFSCommandType, errorReason: String)
    {
        let notification = CommandErrorNotification(sender: self, errorLocalizedString: errorReason)
        sendNotification(notification)
    }
    //****************************************************************
    
    
    private func sendNotification(notification:TFSClientNotification)
    {
        _broadcastCenter.post(notification)
    }
    


   
}
